home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GL_OM.ARJ / GL-OPEN.C < prev    next >
C/C++ Source or Header  |  1991-03-12  |  3KB  |  107 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*
  5. From zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!cunixf.cc.columbia.edu
  6. !cs.columbia.edu!cooper Tue Jan 29 22:07:18 EST 1991
  7.  
  8.  
  9. This C program will take a file of GL format and dissolve it into its
  10. components.  It is not guaranteed, of course.  it also produces a file
  11. listing all the files it creates, so you can re-assemble them using a
  12. program that follows.
  13. You may want to modify this to use command-line arguments.
  14. */
  15.  
  16. /* Directory entry in GL file */
  17. #if 0
  18. struct s_entry {
  19.   long bignum;
  20.   char name[13];
  21. }
  22. #endif
  23.  
  24. /* We don't use it, since some C's make it more than 17 bytes... */
  25.  
  26. main()
  27. {
  28.   short top;  /* size of directory */
  29.   unsigned char *entry;  /* list of directory entries */
  30.   char glfile[100], listfile[100];
  31.   FILE *fp,  /* gl file */
  32.        *listfp;  /* list of files in directory */
  33.   FILE *newfp;   /* opened for each extracted file */
  34.   int num_ents = 0;  /* number of files to extract */
  35.   int i;
  36.   long rsize;  /* Size of current file */
  37.   char buff[1024];
  38.   /* For referencing fileds in entry table: */
  39.   long *bignum;
  40.   unsigned char *name;
  41.  
  42.   /* Open required files */
  43.  
  44.   printf("GL file to open? "); scanf("%s",glfile);
  45.   printf("list file? "); scanf("%s",listfile);
  46.  
  47.   fp = fopen(glfile,"r");
  48.   if (fp == NULL)
  49.     {
  50.       printf("couldn't open file %s\n", glfile);
  51.       exit(-1);
  52.     }
  53.   listfp = fopen(listfile,"w");
  54.   if (listfp == NULL)
  55.     {
  56.       fclose(fp);
  57.       printf("couldn't open list file %s\n", listfile);
  58.       exit(-1);
  59.     }
  60.  
  61.   /* Allocate space for directory */
  62.   fread(&top,2,1,fp);
  63.   entry = (unsigned char *)malloc(top);
  64.  
  65.   /* Read in directory */
  66.   fread(entry,top,1,fp);
  67.   num_ents = top/17 - 1;
  68.  
  69.   /* Create file list */
  70.   for (name = entry+4, i = 0; i < num_ents; i++, name+=17)
  71.     fprintf(listfp, "%s\n", name);
  72.   fclose(listfp);
  73.  
  74.   /* Get file size */
  75.   /* Put it in last (zeroed) directory entry, for easy algorithm */
  76.   fseek(fp, 0L, 2);
  77.   bignum = (long *)&entry[num_ents*17];
  78.   *bignum = ftell(fp);
  79.  
  80.   /* Go to first file */
  81.   bignum = (long *)entry;
  82.   fseek(fp, *bignum, 0);
  83.  
  84.   /* Extract each file */
  85.   for (name = entry+4, i = 0; i < num_ents;
  86.        i++, name+=17, bignum = (long *)(name-4))
  87.     {
  88.       newfp = fopen(name, "w");
  89.       /* File size if address of next file (or end) minus this address */
  90.       rsize = *(long *)((char *)bignum + 17) - *bignum;
  91.  
  92.       /* Copy file, one "block" at a time */
  93.       while(rsize > sizeof(buff))
  94.     {
  95.       fread(buff, sizeof(buff), 1, fp);
  96.       fwrite(buff, sizeof(buff), 1, newfp);
  97.       rsize -= sizeof(buff);
  98.     }
  99.       fread(buff, rsize, 1, fp);
  100.       fwrite(buff, rsize, 1, newfp);
  101.       fclose(newfp);
  102.     }
  103. fclose(fp);
  104. free(entry);
  105. }
  106.  
  107.